app.controller("noticeController",function($scope, $controller, $location, noticeService, noticeCategoryService){
    $controller('baseController',{$scope:$scope, baseService:noticeService});

    $scope.editIsuse = function (id, val, msg) {
        var bool = true;
        if (msg) {
            bool = window.confirm(msg);
        }
        if (bool) {
            var queryJson = {
                entity: {
                    id: id,
                    isuse: val
                }
            };
            noticeService.update(queryJson).success(function (res) {
                $scope.resHandler(res, function (res) {
                    alert("修改成功");
                    $scope.findPage();
                })
            })
        }

    };

    $scope.batchEditIsuse = function (val, msg) {
        var bool = true;
        if (msg) {
            bool = window.confirm(msg);
        }
        if (bool) {
            var queryJson = {
                entity: {
                    isuse: val
                },
                ids : $scope.ids
            };
            noticeService.updateManyByIds(queryJson).success(function (res) {
                $scope.resHandler(res, function (res) {
                    alert("修改成功");
                    $('.js_checkbox').prop("checked", false);
                    $scope.findPage();
                })
            })
        }

    };

    $scope.edit = function(direct){
        var entity = $scope.entity;
        entity.content = CKEDITOR.instances.content.getData();
        if(!entity.title){
            alert("请输入标题");
            return;
        }
        if(!entity.categoryId){
            alert("请选择分类");
            return;
        }
        if(!entity.content){
            alert("请输入内容");
            return;
        }
        noticeService.edit({entity:$scope.entity}).success(function (res) {
            $scope.resHandler(res, function (res) {
                if(direct !== undefined){
                    alert("发布成功");
                    location.href = direct;
                }else {
                    location.reload();
                }
            })
        })
    };

    $scope.listInit = function () {
        noticeCategoryService.findAll().success(function (res) {
            $scope.resHandler(res, function (res) {
                $scope.categoryList = res.data;
            });
        });
        $scope.findPage();
    };

    $scope.editInit = function () {

        noticeCategoryService.findAll().success(function (res) {
            $scope.resHandler(res, function (res) {
                $scope.categoryList = res.data;
            });
        });

        $(function() {
            //Initialize Select2 Elements
            $('.select2').select2();
            CKEDITOR.replace('content');

        })

        $params = $location.search();
        if($params['id'] !== undefined) {
            var queryJson = {
                entity : {
                    id : $params['id']
                }
            };
            noticeService.find(queryJson).success(function (res) {
                $scope.resHandler(res, function () {
                    $scope.entity = res.data;
                    CKEDITOR.instances.content.setData($scope.entity.content);
                    $scope.entity.categoryId = $scope.entity.categoryId.toString();
                    $("#categoryId").val($scope.entity.categoryId).select2();
                });
            });
        }
    };

});
